package com.vzoom.logdesensitization.strategyimpl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vzoom.logdesensitization.DesensitizationStrategy;
import com.vzoom.logdesensitization.util.ObjectMappers;
import org.springframework.beans.factory.InitializingBean;

import java.io.IOException;

public class JsonStrDesensitizationStrategy implements DesensitizationStrategy, InitializingBean {

    private static ObjectMapper objectMapper;

    @Override
    public String desensitization(Object origin) {
        if (origin instanceof String) {
            try {
                Object jsonNode = objectMapper.readValue((String) origin, Object.class);
                return objectMapper.writeValueAsString(jsonNode);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            return String.valueOf(origin);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        objectMapper = ObjectMappers.getJsonStrObjectMapper();
    }
}
